//
//  PrefController.m
//  MouseWarp
//
//  Copyright 2006 Contributors. All rights reserved.
//
//  License: BSD
//
//  Contributors:
//		Ritchie Argue
//      David Dauer
//      Jesper
//
//  Revisions:
//      2006-03-12 Created.
//		2006-12-22 MouseWarp created

#import "PrefController.h"
#import "ShortcutRecorder.h"
#import "PTHotKeyCenter.h"
#import "PTHotKey.h"


@implementation PrefController

-(IBAction)
configureGlobalHotKeys {
	if (fwdGlobalHotKey != nil) {
		[[PTHotKeyCenter sharedCenter] unregisterHotKey: fwdGlobalHotKey];
		[fwdGlobalHotKey release];
		fwdGlobalHotKey = nil;
	}
	
	fwdGlobalHotKey = [[PTHotKey alloc] initWithIdentifier:@"MWHotkey"
											   keyCombo:[PTKeyCombo keyComboWithKeyCode:[shortcutRecorder keyCombo].code
																			  modifiers:[shortcutRecorder cocoaToCarbonFlags:[shortcutRecorder keyCombo].flags]]];
	
	[fwdGlobalHotKey setTarget: self];
	[fwdGlobalHotKey setAction: @selector(hitHotKey:)];
	
	[[PTHotKeyCenter sharedCenter] registerHotKey: fwdGlobalHotKey];

	// shifted hotKey
	if (revGlobalHotKey != nil) {
		[[PTHotKeyCenter sharedCenter] unregisterHotKey: revGlobalHotKey];
		[revGlobalHotKey release];
		revGlobalHotKey = nil;
	}
	
	revGlobalHotKey = [[PTHotKey alloc] initWithIdentifier:@"MWShiftHotKey"
													keyCombo:[PTKeyCombo keyComboWithKeyCode:[shortcutRecorder keyCombo].code
																				   modifiers:[shortcutRecorder cocoaToCarbonFlags:([shortcutRecorder keyCombo].flags | NSShiftKeyMask)]]];

	[revGlobalHotKey setTarget:self];
	[revGlobalHotKey setAction:@selector(hitHotKey:)];
	
	[[PTHotKeyCenter sharedCenter] registerHotKey: revGlobalHotKey];
}


-(void)
awakeFromNib {
	[self configureGlobalHotKeys];
}


#pragma mark -

-(void)
shortcutRecorder:(ShortcutRecorder *)aRecorder keyComboDidChange:(KeyCombo)newKeyCombo {
	if (aRecorder == shortcutRecorder) {
		NSLog(@"setting combo to code: %x, flags: %x", newKeyCombo.code, newKeyCombo.flags);
		
		[self configureGlobalHotKeys];
	}
}


#pragma mark -

// if hit twice in doubleclick time, bring the cursor home
-(void)
hitHotKey:(PTHotKey *)hotKey {
	// get all screens
	NSArray *screens = [NSScreen screens];
	if ([screens count] == 1) {
		return;																	// single screen, so bail early
	}
	
	// have two or more screens
	NSPoint location = [NSEvent mouseLocation];
	
	int i;
	for (i = 0; i < [screens count]; i++) {
		NSScreen *screen = [screens objectAtIndex:i];
		
		if (NSPointInRect(location, [screen frame])) {
			// found the cursor
			
			int targetIndex;
			if (hotKey == fwdGlobalHotKey) {
				// move the cursor forward
				targetIndex = (i + 1) % [screens count];
			} else {
				// move the cursor backward
				targetIndex = (i - 1) % [screens count];
			}
			NSScreen *targetScreen = [screens objectAtIndex:targetIndex];
			NSRect targetFrame = [targetScreen frame];
			NSPoint targetCentre = NSMakePoint(targetFrame.size.width / 2.0, targetFrame.size.height / 2.0);			
			NSPoint targetPoint = NSMakePoint(targetFrame.origin.x + targetCentre.x, targetFrame.origin.y + targetCentre.y);
			
			// I hate this ns/cg crap. now we have to flip the y-axis
			float height = [[NSScreen mainScreen] frame].size.height;
			
			CGPoint cgTargetPoint = CGPointMake(targetPoint.x, height - targetPoint.y);
			CGWarpMouseCursorPosition(cgTargetPoint);
			
			break;
		}
	}	
}

@end
